package com.example.mymusicdisplay.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.databinding.MusicDiscBinding;

public class PlayMusicView extends ConstraintLayout {

    private Context mContext;

    private MusicDiscBinding mMusicDiscBinding;

    private Animation mPlayMusicAnim, mPlayNeedleAnim, mStopNeedleAnim;

    public PlayMusicView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PlayMusicView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        // MediaPlayer
        mContext = context;
        mMusicDiscBinding = DataBindingUtil
            .inflate(LayoutInflater.from(mContext), R.layout.music_disc, this, false);
        /**
         * 1、定义所需要执行的动画
         *      1、光盘转动的动画
         *      2、指针指向光盘的动画
         *      3、指针离开光盘的动画
         * 2、startAnimation
         */
        mPlayMusicAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_music_anim);
        mPlayNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.play_needle_anim);
        mStopNeedleAnim = AnimationUtils.loadAnimation(mContext, R.anim.stop_needle_anim);
        addView(mMusicDiscBinding.getRoot());
    }

    /**
     * 播放音乐
     */
    public void startAnimal() {
        mMusicDiscBinding.playingDisk.startAnimation(mPlayMusicAnim);
        mMusicDiscBinding.playingNeedle.startAnimation(mPlayNeedleAnim);
    }

    /**
     * 停止播放
     */
    public void stopAnimal() {
        mMusicDiscBinding.playingDisk.clearAnimation();
        mMusicDiscBinding.playingNeedle.startAnimation(mStopNeedleAnim);
    }

    /**
     * 设置光盘中显示的音乐封面图片
     */
    public void setMusicIcon(Bitmap bitmap) {
        Glide.with(mContext)
            .load(bitmap)
            .into(mMusicDiscBinding.playingMusicCover);
    }

    /**
     * 设置光盘中显示的音乐封面图片
     */
    public void setMusicIcon(Uri uri) {
        Glide.with(mContext)
            .load(uri)
            .into(mMusicDiscBinding.playingMusicCover);
    }

}